package org.dataSource;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataBase {

	private static final Boolean unknownDataInclusion = false;
	private static final String databaseA = "/adult.data";
	private static final String databaseB = "/adult.test";
	private static Logger logger = LoggerFactory.getLogger(DataBase.class);

	public ArrayList<Record> readAllRecords() {
		logger.info("Rozpoczęto wczytywanie rekordów");
		Long startTime = System.nanoTime();
		
		ArrayList<Record> records = new ArrayList<Record>();
		
		records.addAll(this.readDataBase(databaseA));
		records.addAll(this.readDataBase(databaseB));
		
		logger.info("Zakończono wczytywanie " + records.size() + " z czasem "
				+ (System.nanoTime() - startTime) / 1000000 + " ms");
		return records;
	}
	
	private ArrayList<Record> readDataBase(String name)
	{
		ArrayList<Record> records = new ArrayList<Record>();
		URI newPath = null;
		try {
			newPath = this.getClass().getResource(name).toURI();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String line = null;
		BufferedReader reader;
		try {
			reader = new BufferedReader(new FileReader(newPath.getPath()));

			while ((line = reader.readLine()) != null && !line.isEmpty() && !line.equals("")) {

				if(line.startsWith("|")) continue;
				
				String[] s = line.split(", ");

				Record r = new Record();
				r.setAge(Integer.parseInt(s[0]));
				r.setWorkClass(s[1]);
				r.setFnlwgt(Integer.parseInt(s[2]));
				r.setEducation(s[3]);
				r.setEducationYears(Integer.parseInt(s[4]));
				r.setMaritalStatus(s[5]);
				r.setOccupation(s[6]);
				r.setRelationship(s[7]);
				r.setRace(s[8]);
				r.setSex(s[9]);
				r.setCapitalGain(Integer.parseInt(s[10]));
				r.setCapitalLoss(Integer.parseInt(s[11]));
				r.setHoursPerWeek(Integer.parseInt(s[12]));
				r.setNativeCountry(s[13]);
				r.setIncome(s[14]);

				if (unknownDataInclusion) records.add(r);
				else if(!(r.getWorkClass().contains("?") || r.getEducation().contains("?")
						|| r.getMaritalStatus().contains("?") || r.getOccupation().contains("?") ||
						r.getRelationship().contains("?") || r.getRace().contains("?") || 
						r.getSex().contains("?") || r.getNativeCountry().contains("?")))
				records.add(r);
				 
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			System.err.println(line);
			System.err.println("Ilość wczytanych rekordów: ");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return records;
	}
	
}